/*
	管理员列表
*/
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

function init(layer, form, table){

	//ajax data	
	var ajaxData = {
		aId: adminLoginedInfo.aId,
		aToken: adminLoginedInfo.aToken
	};

	function createAdminDataTable(adminData){

		$('#dataSize').text(adminData.length);

		var tableData = [];
		for (var i = adminData.length - 1; i >= 0; i--) {
			tableData.push({
				adminID: adminData[i].aId,
				name: adminData[i].aName,
				email: adminData[i].aMail,
				phone: adminData[i].aPhone,
				role: adminData[i].aRole,
				create_time: formatTime( adminData[i].formatTime ),
				login_time: formatTime( adminData[i].lastLoginTime ),
				login_ip: adminData[i].lastLoginIp
			});
		}
		table.render({
			elem: '#tableContent',
			limit: 30, //分页
			height: 680,
			page: true,
			cols:[[ //表头
				{field: 'adminID', title: 'AID', sort: true, fixed: 'left', width:120},
				{field: 'name', title: '登录名'},
				{field: 'email', title: '邮箱'},
				{field: 'phone', title: '联系方式', sort: true},
				{field: 'role', title: '角色'},
				{field: 'create_time', title: '注册时间', sort: true},
				{field: 'login_time', title: '最近登录时间'},
				{field: 'login_ip', title: '最近登录地址', sort: true},
				{field: 'option', title: '操作', toolbar:'#toolbar'}
			]],
			data: tableData
		});
	}

	function getAdmins(ajaxData){
		/*
			获取管理员列表
		*/
		var loadIndex = layer.load(1, { shade: [0.5, '#000'] });

		$.ajax({
			url: BASE_SERVICEURL + URlS.superAdmin,
			type:'GET',
			data: ajaxData,
			success: function(responseData){
				if(responseData.msg.code == 200){
					//console.log(responseData);
					createAdminDataTable(responseData.data);
					layer.close(loadIndex);
				}else{
					layer.close(loadIndex);
					layer.msg(responseData.msg.msg, {time: 2000});
				}
			},
			error: function(responseData){
				layer.close(loadIndex);
				layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
					//跳转500
					location.href = './500.html';
				}})
			}
		}); 
	};

	//详情查看
	table.on('tool(admin-list-table)', function(obj){
		var data = obj.data;   //本行数据
		var layEvent = obj.event; 
		var tr = obj.tr;  //当前行
		//console.log(data)
		if(layEvent == 'seeDetatil'){  //查看详情
			x_admin_show('查看详情', 'admin-detail.html?spmId='+data.adminID+'', 1200, 800);
		};
	});

	getAdmins(ajaxData);
};






// dom 
$(function(){
	layui.use(['layer', 'form', 'table'], function(){
		var layer = layui.layer, form = layui.form, table = layui.table;
		init(layer, form, table);
	});
});





